package com.fanling.venus.web.controller.project;

import com.fanling.venus.common.annotation.Log;
import com.fanling.venus.common.base.AjaxResult;
import com.fanling.venus.common.enums.BusinessType;
import com.fanling.venus.frame.web.BaseController;
import com.fanling.venus.frame.web.TableDataInfo;
import com.fanling.venus.project.domain.EpointProject;
import com.fanling.venus.project.service.EpointProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/project/epointproject")
public class EpointProjectController extends BaseController {

    private String prefix = "project/epointproject";

    @Autowired
    private EpointProjectService epointProjectService;

    @GetMapping()
    public String project() {
        return prefix + "/epointproject";
    }

    @GetMapping("/add")
    public String add()
    {
        return prefix + "/epointproject-add";
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit/{projectId}")
    public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        mmap.put("epointproject", epointProjectService.selectProjectById(projectId));
        return prefix + "/epointproject-edit";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EpointProject epointProject) {
        startPage();
        List<EpointProject> list = epointProjectService.selectProjectList(epointProject);
        return getDataTable(list);
    }

    /**
     * 新增项目
     */
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EpointProject project) {
        //判断菜单名称
        String uniqueFlag = epointProjectService.checkProjectNameUnique(project);
        if ("1".equals(uniqueFlag)) {
            return error(1, "项目名称已经存在！");
        }
        return toAjax(epointProjectService.insertMenu(project));
    }

    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EpointProject project) {
        return toAjax(epointProjectService.updateProject(project));
    }

    @PostMapping("/remove/{projectId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("projectId") Long projectId) {
        return toAjax(epointProjectService.deleteProjectById(projectId));
    }

    /**
     * 选择项目树
     */
    @GetMapping("/epointProjectTreeData/{projectId}")
    public String selectEpointProjectTree(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        mmap.put("epointproject", epointProjectService.selectProjectById(projectId));
        return prefix + "/epointproject-tree";
    }
    /**
     * 加载所有菜单列表树
     */
    @GetMapping("/epointProjectTreeData")
    @ResponseBody
    public List<Map<String, Object>> epointProjectTreeData()
    {
        List<Map<String, Object>> tree = epointProjectService.epointProjectTreeData();
        return tree;
    }
}
